import 'package:bookkeeping/db/db_helper.dart';
import 'package:bookkeeping/login/login.dart';
import 'package:bookkeeping/login/userInfo.dart';
import 'package:bookkeeping/widgets/LoginTextInput.dart';
import 'package:bookkeeping/widgets/custom_button.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';


class Register extends StatefulWidget {

  Register({Key key}) : super(key: key);

  @override
  _RegisterState createState() => _RegisterState();
}

class _RegisterState extends State<Register> {

  static TextEditingController _accountController = new TextEditingController();
  static TextEditingController _passwordController = new TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: true,
      body: Container(
        width: double.maxFinite,
        height: double.maxFinite,
        padding: EdgeInsets.only(left: 50, right: 50),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text('注册新账号', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
            Image.asset('assets/images/icons/note.jpg', height: 100),
            SizedBox(height: 40),
            LoginTextInput(
              hintText: '请输入账号',
              textEditingController: _accountController,
            ),
            SizedBox(height: 20),
            LoginTextInput(
              hintText: '请输入密码',
              textEditingController: _passwordController,
            ),
            SizedBox(height: 60),
            Padding(
                padding: EdgeInsets.only(left: 20, right: 20),
                child: CustomButton(
                  color: Colors.teal,
                  content: '注册',
                  onTap: (){
                    _register();
                  },
                )
            ),
            SizedBox(height: 20),
            GestureDetector(
              onTap: (){
                Navigator.push<dynamic>(
                  context,
                  MaterialPageRoute<dynamic>(
                    builder: (BuildContext context) => Login()
                  ),
                );
              },
              child: Text('去登录 >'),
            )
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  _register() async {
    if(_accountController.text == ''){
      showToast('账号不可为空');
    }else if(_passwordController.text == ''){
      showToast('密码不可为空');
    }else {

      UserInfo userInfo = await dbHelp.getUserInfoByAccount(_accountController.text);
      List<UserInfo> userInfoList = await dbHelp.queryAll();
      if(userInfo != null){
        showToast('该昵称已被占用');
      } else {
        if(userInfoList == null){
          dbHelp.insert(new UserInfo(id: 1, userAccount: _accountController.text, password: _passwordController.text));
        }else {
          dbHelp.insert(new UserInfo(id: userInfoList.length + 1, userAccount: _accountController.text, password: _passwordController.text));
        }
        showToast('注册成功!');
        Future.delayed(Duration(seconds: 2), (){
          Navigator.push<dynamic>(
            context,
            MaterialPageRoute<dynamic>(
                builder: (BuildContext context) => Login()
            ),
          );
        });
      }
    }
  }

}